/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package au.com.domlogic.pvc.xhtml.servlet;

import au.com.domlogic.pvc.core.element.PVCElement;
import au.com.domlogic.pvc.core.servlet.URLCreator;
import au.com.domlogic.pvc.xhtml.attr.HrefAttrMold;
import au.com.domlogic.pvc.xhtml.document.XHTMLStrictDocument;
import au.com.domlogic.pvc.xhtml.element.BaseElementMold;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

/**
 *
 * @author Domenic Figliomeni
 *
 * This filter wraps the incoming request with a XHTMLStrictRequestWrapper.
 * This allows or places an XHTMLStrict Document on the request.  Web tier
 * components can make use of DOM methods to manipulate the document as it makes
 * its way to the serializer and ultimately the response.
 */
public class XHTMLStrictRequestFilter implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConf) throws ServletException {
        this.filterConfig = filterConf;
    }

    @Override
    public void doFilter(ServletRequest request,
            ServletResponse response, FilterChain chain) throws IOException,
            ServletException {

        try {
            if (request instanceof HttpServletRequest) {
                HttpServletRequest httpServletRequest = (HttpServletRequest) request;
                XHTMLStrictRequestWrapper wrapper;
                if (httpServletRequest instanceof XHTMLStrictRequestWrapper) {
                    wrapper = (XHTMLStrictRequestWrapper) httpServletRequest;
                } else {
                    wrapper = new XHTMLStrictRequestWrapper(httpServletRequest);
                }
                XHTMLStrictDocument document = wrapper.getDocument();
                //Get the head as a pvc element we can decorate
                PVCElement head = document.getHead();
                //Get the url to use as the base
                String baseUrl = (new URLCreator()).getBaseUrlString(wrapper);
                //Construct a model for a Base Element and add a href attr model
                BaseElementMold base = new BaseElementMold();
                base.addAttrMold(new HrefAttrMold(baseUrl));
                head = base.new ElementDecorator(head);
                head.gluePVC();
                request = wrapper;
            } else {
                throw new ServletException("The request was not a HttpServletRequest."
                        + "  It was a\t" + request.getClass().getName());
            }
        } catch (ParserConfigurationException e) {
            throw new ServletException(e);
        } catch (Exception e) {
            throw new ServletException(e);
        } finally {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        this.filterConfig = null;
    }

    public FilterConfig getFilterConfig() {
        return filterConfig;
    }

    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }
}
